/**
* @author:kellen
* @contact:xiekellen@hotmail.com
* @company:achivo-gz
* @created on:
* @modified on:
*/

package com.sasis.dao.ibatis;

import java.sql.SQLException;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.sasis.dao.ibatis.BaseDaoiBATIS;
import com.sasis.model.SwitchRecord;
import com.sasis.dao.SwitchRecordDao;

import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.ibatis.SqlMapClientCallback;

public class SwitchRecordDaoiBatis extends BaseDaoiBATIS implements SwitchRecordDao {
	
    /**
     * @see com.sasis.dao.SwitchRecordDao#getSwitchRecords(com.sasis.model.SwitchRecord)
     */
    public List getSwitchRecords(final SwitchRecord switchRecord) {
          return getSqlMapClientTemplate().queryForList("getSwitchRecords", switchRecord);
    }
	 /**
     * @see com.sasis.dao.SwitchRecordDao#getSwitchRecordManager(com.sasis.model.SwitchRecord)
     */
    public List getSwitchRecordManager(final SwitchRecord switchRecord) {
          return getSqlMapClientTemplate().queryForList("switchRecordManager", switchRecord);
    }


    /**
     * @see com.sasis.dao.SwitchRecordDao#getSwitchRecord(Integer id)
     */
    public SwitchRecord getSwitchRecord(Integer id) {
        SwitchRecord switchRecord = (SwitchRecord) getSqlMapClientTemplate().queryForObject("getSwitchRecord", id);

        //if (switchRecord == null) {
            //throw new ObjectRetrievalFailureException(SwitchRecord.class, id);
        //}

        return switchRecord;
    }

    /**
     * @see com.sasis.dao.SwitchRecordDao#saveSwitchRecord(SwitchRecord switchRecord)
     */    
    public int saveSwitchRecord(final SwitchRecord switchRecord) {
        Integer id = switchRecord.getId();
        // check for new record
        if (id == null) {
            id = (Integer) getSqlMapClientTemplate().insert("addSwitchRecord", switchRecord);
        } else {
            getSqlMapClientTemplate().update("updateSwitchRecord", switchRecord);
        }
        if( id == null ) {
            throw new ObjectRetrievalFailureException(SwitchRecord.class, id);
        }
        return id.intValue();
    }
    /**
     * @see com.sasis.dao.SwitchRecordDao#saveSwitchRecord(SwitchRecord switchRecord)
     */    
    public void saveSwitchRecords(final SwitchRecord switchRecord) {
    	 final String[] ids=switchRecord.getIds();
     	//String[]  exsistIds=null;
     	final int len = switchRecord.getIds().length;
         
     	final String[] exsistIds = new String[len];
         // check for new record
         //if (null!=ids && 0!=ids.length) {
         	
              //  getSqlMapClientTemplate().update("updatePassengerNotifyInfos", passengerNotifyInfo);
         	//}else{
         	
         	       getSqlMapClientTemplate().execute(new SqlMapClientCallback() { 
         	    	   
                        public Object doInSqlMapClient(SqlMapExecutor executor)
                                throws SQLException { 
                            executor.startBatch();
                            int index=0;
                            String id="";
                            String passengerId="";
                            for(int i=0;i<len;i++)
                            {    
                         	   
                         	     //disjoin the id and passengerId
                       			 index=ids[i].indexOf(",");
                       			 if (index!=0){
                        			   id=ids[i].substring(0,index);
                        			   passengerId=ids[i].substring(index+1);
                        			 }else{
                        				 id="";
                        				 passengerId=ids[i].substring(1);
                        			 }
                       			 
                        			 passengerId=ids[i].substring(index+1);
                         	   
                         	   if(null!=id&&!"".equals(id)){
                         		   exsistIds[i]=id;
                         	   }else{
                         		  switchRecord.setPi_id(new Integer(passengerId));	   
                                  executor.insert("addSwitchRecord", switchRecord);
                         	   }
                            }
                            executor.executeBatch();
                            return null;
                        }
                    });
         	       if(null!=exsistIds)switchRecord.setIds(exsistIds);
         	       getSqlMapClientTemplate().update("updateSwitchRecords", switchRecord);
         	  // ids= (String[]) getSqlMapClientTemplate().insert("addPassengerNotifyInfos", passengerNotifyInfo);
         	       
         	//}
         
              
    }

    /**
     * @see com.sasis.dao.SwitchRecordDao#removeSwitchRecord(Integer id)
     */
    public void removeSwitchRecord(SwitchRecord switchRecord) {
        getSqlMapClientTemplate().update("deleteSwitchRecord",  switchRecord);
    }
}
